package org.cxa.cableModemMonitor;
import org.jsoup.nodes.Element;

/**
 * Copyright 2014 opencxa.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author Christopher Armenio
 *
 */
public class DownstreamChannelReading extends ChannelReading
{
	
	private final String modulation;
	private final float snr;
	private final long correctables;
	private final long uncorrectables;
	
	
	public DownstreamChannelReading(int expectedChannelNumIn, Element channelElementIn)
	{
		super(expectedChannelNumIn, channelElementIn.child(0), channelElementIn.child(1), channelElementIn.child(3), channelElementIn.child(4), channelElementIn.child(5));
		
		if( channelElementIn.children().size() != 9 ) throw new IllegalArgumentException(
				String.format("invalid number of children - got %d, expected %d", channelElementIn.children().size(), 9));
		
		this.modulation = channelElementIn.child(2).text();
		
		String snrStrings[] = channelElementIn.child(6).text().split(" ");
		if( (snrStrings.length != 2) || !snrStrings[1].equals("dBmV") ) throw new IllegalArgumentException(
				String.format("invalid snr string - %s", channelElementIn.child(6).text()));
		this.snr = Float.parseFloat(snrStrings[0]);
		
		this.correctables = Long.parseLong(channelElementIn.child(7).text());
		
		this.uncorrectables = Long.parseLong(channelElementIn.child(8).text());
	}
	
	
	@Override
	public String getCsvString()
	{
		return String.format("%d, %s, %s, %d, %f, %f, %f, %d, %d", this.chanNum, this.chanStatus.toString(), this.modulation, this.chanId, this.frequency, this.power, this.snr, this.correctables, this.uncorrectables);
	}
	
	
	@Override
	public String toString()
	{
		return String.format("%d %s %s %d %f %f %f %d %d", this.chanNum, this.chanStatus.toString(), this.modulation, this.chanId, this.frequency, this.power, this.snr, this.correctables, this.uncorrectables);
	}
	
	
	public static String getCsvHeaderString()
	{
		return "Downstream ChanNum, ChanStatus, Modulation, ChanId, Frequency (Hz), Power (dBmV), SNR (dBmV), correctables, uncorrectables";
	}
}
